package main

import (
	"encoding/json"
	"fmt"
	"golang-learning/gorm/model"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
	"log"
)

/**
参考：https://gorm.io/zh_CN/docs/connecting_to_the_database.html
*/
func main() {
	dsn := "base:JieYiYun123@tcp(rm-uf64m74m539h5d7904o.mysql.rds.aliyuncs.com:3306)/base?charset=utf8mb4&parseTime=True&loc=Local"
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			SingularTable: true, // 使用单数表名
		},
	})

	if err != nil {
		fmt.Println(err)
		return
	} else {
		fmt.Println("connection succedssed")
	}
	//defer db.Close()
	// query all

	var baseChnl []model.BaseChnl
	db.Find(&baseChnl)
	fmt.Println(baseChnl)
	fmt.Println(baseChnl[0])

	rs, err := json.Marshal(baseChnl)
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Println("rs:", string(rs))

}
